package top.huhuiyu.ds.sort;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableDemo {

  public static void save(Student student, String file) throws Exception {
    // 对象输出流,可以将实现了Serializable写入文件
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
    // 输出对象
    oos.writeObject(student);
    oos.close();
  }
  
  public static Student load(String file) throws Exception{
    //对象输出流，可以将序列化到文件的对象还原回来
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
    Student student=(Student) ois.readObject();
    ois.close();
    return student;
  }

  public static void main(String[] args) {
    try {
//      SerializableDemo.save(new Student(100, "nmsl"), "student.data");
//      System.out.println("输出到文件完成");
      
     System.out.println(SerializableDemo.load("student.data"));

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
